package com.wlper.lib.view.adapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.wlper.lib.uitls.ViewUtils;

import android.content.Context;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Checkable;
import android.widget.ImageView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

/**
 * 带有标记的ListView适配器。
 * 
 * @author wanliang527
 * @date 2014-6-9
 */
public class SimpleAdapterWithMarker extends SimpleAdapter {
    private Context mContext;
    private int[] mTo;
    private String[] mFrom;
    private List<? extends Map<String, ?>> mData;
    private int mResource;
    private LayoutInflater mInflater;
    private int selectedPos;
    private Object mNormalBg, mSelectedBg;

    private int textSize = 15, textColor = 0;
    private int customTextViewId = -1;

    public SimpleAdapterWithMarker(Context context,
            List<? extends Map<String, ?>> data, int resource, String[] from,
            int[] to) {
        super(context, data, resource, from, to);
        mContext = context;
        mInflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mData = data;
        mResource = resource;
        mFrom = from;
        mTo = to;
    }

    public SimpleAdapterWithMarker(Context context,
            List<? extends Map<String, ?>> data, List<String> oData,
            int resource, String[] from, int[] to) {
        this(context, data, resource, from, to);
        for (int i = 0; i < oData.size(); i++) {
            HashMap<String, Object> map = new HashMap<String, Object>();
            map.put(from[0], oData.get(i));
            ((ArrayList) mData).add(map);
        }
    }

    /**
     * 设置列表项的背景图。
     * 
     * @param normalBg
     *            正常状态时的背景
     * @param selectedBg
     *            被选择时的背景
     */
    public void setItemBackground(Object normalBg, Object selectedBg) {
        mNormalBg = normalBg;
        mSelectedBg = selectedBg;
        notifyDataSetChanged();
    }

    public void customText(int textViewId, int textSize, int textColor) {
        customTextViewId = textViewId;
        this.textSize = textSize;
        this.textColor = textColor;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        return createViewFromResource(position, convertView, parent, mResource);
    }

    /**
     * 获取当前选中的Item位置
     * 
     * @return
     */
    public int getSelectedPos() {
        return selectedPos;
    }

    private View.OnClickListener clickListener = new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            int position = (Integer) v.getTag();
            if (position != selectedPos
                    && (mNormalBg != null && mSelectedBg != null)) {
                selectedPos = position;
                notifyDataSetChanged();
            }
            if (itemClickListener != null) {
                itemClickListener.onItemClick(v, position);
            }

        }
    };

    private View createViewFromResource(final int position, View convertView,
            ViewGroup parent, int resource) {
        View v;
        if (convertView == null) {
            v = mInflater.inflate(resource, parent, false);
        } else {
            v = convertView;
        }
        if (customTextViewId != -1) {
            customView(v, customTextViewId);
        }
        v.setTag(position);
        v.setOnClickListener(clickListener);
        if (position == selectedPos) {
            ViewUtils.setViewBackground(v, mSelectedBg);
        } else {
            ViewUtils.setViewBackground(v, mNormalBg);
        }
        bindView(position, v);
        return v;
    }

    private void customView(View parent, int customViewId) {
        if (parent == null)
            return;
        View v = parent.findViewById(customViewId);
        if (v != null) {
            if (v instanceof TextView) {
                ((TextView) v).setTextColor(mContext.getResources().getColor(
                        textColor));
                ((TextView) v)
                        .setTextSize(TypedValue.COMPLEX_UNIT_SP, textSize);

            }
        } else {
            customViewId = -1;
        }
    }

    private void bindView(int position, View view) {
        final Map dataSet = mData.get(position);
        if (dataSet == null) {
            return;
        }
        final String[] from = mFrom;
        final int[] to = mTo;
        final int count = to.length;

        for (int i = 0; i < count; i++) {
            final View v = view.findViewById(to[i]);
            if (v != null) {
                final Object data = dataSet.get(from[i]);
                String text = data == null ? "" : data.toString();
                if (text == null) {
                    text = "";
                }
                boolean bound = false;

                if (!bound) {
                    if (v instanceof Checkable) {
                        if (data instanceof Boolean) {
                            ((Checkable) v).setChecked((Boolean) data);
                        } else if (v instanceof TextView) {
                            // Note: keep the instanceof TextView check at the
                            // bottom of these
                            // ifs since a lot of views are TextViews (e.g.
                            // CheckBoxes).
                            setViewText((TextView) v, text);
                        } else {
                            throw new IllegalStateException(v.getClass()
                                    .getName()
                                    + " should be bound to a Boolean, not a "
                                    + (data == null ? "<unknown type>"
                                            : data.getClass()));
                        }
                    } else if (v instanceof TextView) {
                        // Note: keep the instanceof TextView check at the
                        // bottom of these
                        // ifs since a lot of views are TextViews (e.g.
                        // CheckBoxes).
                        setViewText((TextView) v, text);
                    } else if (v instanceof ImageView) {
                        if (data instanceof Integer) {
                            setViewImage((ImageView) v, (Integer) data);
                        } else {
                            setViewImage((ImageView) v, text);
                        }
                    } else {
                        throw new IllegalStateException(
                                v.getClass().getName()
                                        + " is not a "
                                        + " view that can be bounds by this SimpleAdapter");
                    }
                }
            }
        }
    }

    private OnItemViewClickListener itemClickListener;

    public void setItemClickListener(OnItemViewClickListener listener) {
        itemClickListener = listener;
    }

    public interface OnItemViewClickListener {
        public void onItemClick(View itemView, int itemViewPos);
    }

    public List<? extends Map<String, ?>> getData() {
        return mData;
    }

}
